using System;
using System.Drawing;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;
using OpenNETCF.Win32;
using OpenNETCF.Windows.Forms;
using WinCE;

namespace PPCHelpReader
{
	/// <summary>
	/// Summary description for HTMLView.
	/// </summary>
	/// 
	public enum EnumWhereNext {
        /// <summary>
        /// 
        /// </summary>
        Prev,
        /// <summary>
        /// 
        /// </summary>
        Next,
        /// <summary>
        /// 
        /// </summary>
        Close,
        /// <summary>
        /// 
        /// </summary>
        Link,
        /// <summary>
        /// 
        /// </summary>
        None};

    /// <summary>
    /// 
    /// </summary>
	public class HTMLView : System.Windows.Forms.Form
	{		
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem mnuTools;
		private System.Windows.Forms.MenuItem mnuTextSize;
		private System.Windows.Forms.MenuItem mnuSmallest;
		private System.Windows.Forms.MenuItem mnuSmall;
		private System.Windows.Forms.MenuItem mnuMedium;
		private System.Windows.Forms.MenuItem mnuLarge;
		private System.Windows.Forms.MenuItem mnuLargest;
		private System.Windows.Forms.MenuItem mnuClearType;
		private System.Windows.Forms.MenuItem mnuFitToWindow;
		private System.Windows.Forms.MenuItem mnuAbout;
		private System.Windows.Forms.MenuItem mnuHelp;
		private System.Windows.Forms.MenuItem mnuPrev;
		private System.Windows.Forms.MenuItem mnuNext;

        /// <summary>
        /// 
        /// </summary>
		public EnumWhereNext	WhereNext=EnumWhereNext.Close;
        /// <summary>
        /// 
        /// </summary>
		public string	Link="";
		private string	m_Anchor="";
		private string	regClearType="ClearType";
		private string	regTextSize="TextSize";		
		private string	regFitToWindow="FitToWindow";		
		private System.Windows.Forms.MenuItem menuLocation;		
		private OpenNETCF.Windows.Forms.WebBrowser webBrowser1;

        /// <summary>
        /// Initializes a new instance of the <see cref="HTMLView"/> class.
        /// </summary>
		public HTMLView()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			
			webBrowser1=new OpenNETCF.Windows.Forms.WebBrowser();			
			webBrowser1 = new OpenNETCF.Windows.Forms.WebBrowser();
			webBrowser1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;						
			webBrowser1.Location=new Point(0,0);			
			webBrowser1.Size=new Size(Width,Height-1);
			webBrowser1.Text = "PPCHelpReader";
			webBrowser1.EnableScripting=true;
			webBrowser1.EnableContextMenu=true;
            webBrowser1.Navigating += new OpenNETCF.Windows.Forms.WebBrowserNavigatingEventHandler(Navigating);
            webBrowser1.Navigated += new OpenNETCF.Windows.Forms.WebBrowserNavigatedEventHandler(Navigated);
            webBrowser1.DocumentCompleted += new OpenNETCF.Windows.Forms.WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
			webBrowser1.DocumentTitleChanged+=new EventHandler(DocumentTitleChanged);
			Controls.Add(webBrowser1);			
			Cursor.Current=Cursors.WaitCursor;
								
			RegistryKey RegKey=null;
			try
			{
				RegKey=Registry.CurrentUser.OpenSubKey(@"Software\PPCHelpReader.NET");				

				// Get & Set Text Size
				SetTextSize(Convert.ToInt32(RegKey.GetValue(regTextSize,2).ToString()));

				// Get & Set ClearType
				bool bBool=false;
				int iInt=Convert.ToInt32(RegKey.GetValue(regClearType,1));
				if (iInt!=0)
					bBool=true;
				SetClearType(bBool);

				// Get & Set 'Fit To Window'
				iInt=Convert.ToInt32(RegKey.GetValue(regFitToWindow,1));
				if (iInt!=0)
					bBool=true;				
				SetFitToWindow(bBool);
				RegKey.Close();
			}
			catch(Exception ex)
			{
				RegKey.Close();
				Debug.WriteLine(ex.Message);
			}			
		}

		private string LocationString="";
        /// <summary>
        /// Sets the title.
        /// </summary>
        /// <param name="Title">The title.</param>
		public void SetTitle(string Title)
		{
			LocationString=Title;			
		}

        /// <summary>
        /// Sets the HTML text.
        /// </summary>
        /// <param name="HTMLText">The HTML text.</param>
		public void SetHTMLText(string HTMLText)
		{
			m_Anchor="";
			webBrowser1.Source=HTMLText;
		}

		private string m_Url="";
        /// <summary>
        /// Sets the URL.
        /// </summary>
        /// <param name="Url">The URL.</param>
		public void SetUrl(string Url)
		{
			m_Anchor="";
			m_Url=Url;
		}

        /// <summary>
        /// Navs the buttons.
        /// </summary>
        /// <param name="bEnableNavButtons">if set to <c>true</c> [b enable nav buttons].</param>
		public void NavButtons(bool bEnableNavButtons)
		{
			mnuNext.Enabled=bEnableNavButtons;
			mnuPrev.Enabled=bEnableNavButtons;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.mnuTools = new System.Windows.Forms.MenuItem();
            this.mnuClearType = new System.Windows.Forms.MenuItem();
            this.mnuTextSize = new System.Windows.Forms.MenuItem();
            this.mnuSmallest = new System.Windows.Forms.MenuItem();
            this.mnuSmall = new System.Windows.Forms.MenuItem();
            this.mnuMedium = new System.Windows.Forms.MenuItem();
            this.mnuLarge = new System.Windows.Forms.MenuItem();
            this.mnuLargest = new System.Windows.Forms.MenuItem();
            this.mnuFitToWindow = new System.Windows.Forms.MenuItem();
            this.mnuPrev = new System.Windows.Forms.MenuItem();
            this.mnuNext = new System.Windows.Forms.MenuItem();
            this.menuLocation = new System.Windows.Forms.MenuItem();
            this.mnuHelp = new System.Windows.Forms.MenuItem();
            this.mnuAbout = new System.Windows.Forms.MenuItem();
            this.SuspendLayout();
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.Add(this.mnuTools);
            this.mainMenu1.MenuItems.Add(this.mnuPrev);
            this.mainMenu1.MenuItems.Add(this.mnuNext);
            this.mainMenu1.MenuItems.Add(this.menuLocation);
            this.mainMenu1.MenuItems.Add(this.mnuHelp);
            // 
            // mnuTools
            // 
            this.mnuTools.MenuItems.Add(this.mnuClearType);
            this.mnuTools.MenuItems.Add(this.mnuTextSize);
            this.mnuTools.MenuItems.Add(this.mnuFitToWindow);
            this.mnuTools.Text = "Tools";
            // 
            // mnuClearType
            // 
            this.mnuClearType.Text = "ClearType";
            this.mnuClearType.Click += new System.EventHandler(this.mnuClearType_Click);
            // 
            // mnuTextSize
            // 
            this.mnuTextSize.MenuItems.Add(this.mnuSmallest);
            this.mnuTextSize.MenuItems.Add(this.mnuSmall);
            this.mnuTextSize.MenuItems.Add(this.mnuMedium);
            this.mnuTextSize.MenuItems.Add(this.mnuLarge);
            this.mnuTextSize.MenuItems.Add(this.mnuLargest);
            this.mnuTextSize.Text = "Text Size";
            // 
            // mnuSmallest
            // 
            this.mnuSmallest.Text = "Smallest";
            this.mnuSmallest.Click += new System.EventHandler(this.mnuSmallest_Click);
            // 
            // mnuSmall
            // 
            this.mnuSmall.Text = "Small";
            this.mnuSmall.Click += new System.EventHandler(this.mnuSmall_Click);
            // 
            // mnuMedium
            // 
            this.mnuMedium.Text = "Medium";
            this.mnuMedium.Click += new System.EventHandler(this.mnuMedium_Click);
            // 
            // mnuLarge
            // 
            this.mnuLarge.Text = "Large";
            this.mnuLarge.Click += new System.EventHandler(this.mnuLarge_Click);
            // 
            // mnuLargest
            // 
            this.mnuLargest.Text = "Largest";
            this.mnuLargest.Click += new System.EventHandler(this.mnuLargest_Click);
            // 
            // mnuFitToWindow
            // 
            this.mnuFitToWindow.Text = "Fit To Window";
            this.mnuFitToWindow.Click += new System.EventHandler(this.mnuFitToWindow_Click);
            // 
            // mnuPrev
            // 
            this.mnuPrev.Text = " < ";
            this.mnuPrev.Click += new System.EventHandler(this.mnuPrev_Click);
            // 
            // mnuNext
            // 
            this.mnuNext.Text = " > ";
            this.mnuNext.Click += new System.EventHandler(this.mnuNext_Click);
            // 
            // menuLocation
            // 
            this.menuLocation.Text = "Loc";
            this.menuLocation.Click += new System.EventHandler(this.menuLocation_Click);
            // 
            // mnuHelp
            // 
            this.mnuHelp.MenuItems.Add(this.mnuAbout);
            this.mnuHelp.Text = "Help";
            // 
            // mnuAbout
            // 
            this.mnuAbout.Text = "About...";
            this.mnuAbout.Click += new System.EventHandler(this.mnuAbout_Click);
            // 
            // HTMLView
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.ClientSize = new System.Drawing.Size(240, 268);
            this.MaximizeBox = false;
            this.Menu = this.mainMenu1;
            this.MinimizeBox = false;
            this.Name = "HTMLView";
            this.Text = "PPCHelpReader.NET";
            this.Load += new System.EventHandler(this.HTMLView_Load);
            this.Closed += new System.EventHandler(this.HTMLView_Closed);
            this.GotFocus += new System.EventHandler(this.HTMLView_GotFocus);
            this.ResumeLayout(false);

		}
		#endregion

		private void mnuAbout_Click(object sender, System.EventArgs e)
		{
			Dialogs.ShowDialog(new About(),this);			
            // new About().ShowDialog();
		}

		private void SetClearType(bool bEnableClearType)
		{			
			RegistryKey RegKey=Registry.CurrentUser.CreateSubKey(@"Software\PPCHelpReader.NET");
			RegKey.SetValue(regClearType,Convert.ToInt32(bEnableClearType));
			RegKey.Close();

			mnuClearType.Checked=bEnableClearType;
			webBrowser1.EnableClearType=bEnableClearType;			
		}

		private void mnuClearType_Click(object sender, System.EventArgs e)
		{
			SetClearType(!mnuClearType.Checked);
		}

		private void SetFitToWindow(bool bEnableFitToWindow)
		{
			RegistryKey RegKey=Registry.CurrentUser.CreateSubKey(@"Software\PPCHelpReader.NET");			
			RegKey.SetValue(regFitToWindow,Convert.ToInt32(bEnableFitToWindow));			
			RegKey.Close();
			webBrowser1.EnableShrink=bEnableFitToWindow;
			mnuFitToWindow.Checked=bEnableFitToWindow;
		}

		private void mnuFitToWindow_Click(object sender, System.EventArgs e)
		{			
			SetFitToWindow(!mnuFitToWindow.Checked);
		}

		private void SetTextSize(int ZoomLevel)
		{
			RegistryKey RegKey=Registry.CurrentUser.CreateSubKey(@"Software\PPCHelpReader.NET");
			RegKey.SetValue(regTextSize,ZoomLevel);
			RegKey.Close();
			webBrowser1.ZoomLevel=ZoomLevel;
			mnuSmallest.Checked=(ZoomLevel==0);
			mnuSmall.Checked=(ZoomLevel==1);
			mnuMedium.Checked=(ZoomLevel==2);
			mnuLarge.Checked=(ZoomLevel==3);
			mnuLargest.Checked=(ZoomLevel==4);
		}

		private void mnuSmallest_Click(object sender, System.EventArgs e)
		{
			SetTextSize(0);
		}

		private void mnuSmall_Click(object sender, System.EventArgs e)
		{
			SetTextSize(1);
		}

		private void mnuMedium_Click(object sender, System.EventArgs e)
		{
			SetTextSize(2);
		}

		private void mnuLarge_Click(object sender, System.EventArgs e)
		{
			SetTextSize(3);
		}

		private void mnuLargest_Click(object sender, System.EventArgs e)
		{
			SetTextSize(4);
		}

		private void mnuPrev_Click(object sender, System.EventArgs e)
		{
			WhereNext=EnumWhereNext.Prev;
			base.Close();
		}

		private void mnuNext_Click(object sender, System.EventArgs e)
		{			
			WhereNext=EnumWhereNext.Next;			
			Close();
		}

		private void HTMLView_Closed(object sender, System.EventArgs e)
		{				
			Close();
		}

        /// <summary>
        /// Sets the anchor.
        /// </summary>
        /// <param name="Anchor">The anchor.</param>
		public void SetAnchor(string Anchor)
		{
			m_Anchor=Anchor;			
		}

		private void Navigating(object sender, OpenNETCF.Windows.Forms.WebBrowserNavigatingEventArgs e)
		{
			// if (e.Url.ToLower().StartsWith("javascript:")) return;

			Link=e.Url;
			WhereNext=EnumWhereNext.Close;
			if (e.IsHotSpot)
			{                
				WhereNext=EnumWhereNext.Link;
                if (!Link.StartsWith("#"))
				    Close();
			}
		}

        private void Navigated(object sender, OpenNETCF.Windows.Forms.WebBrowserNavigatedEventArgs e)
		{
			Cursor.Current=Cursors.Default;
		}

        private void DocumentCompleted(object sender, OpenNETCF.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
		{
			Cursor.Current=Cursors.Default;	
		}

		private void DocumentTitleChanged(object sender, EventArgs e)
		{			
		}

		private void HTMLView_Load(object sender, System.EventArgs e)
		{			
			WhereNext=EnumWhereNext.Close;			
			webBrowser1.JumpToAnchor(m_Anchor);
			webBrowser1.Focus();

			if (m_Url!="") 
				webBrowser1.Navigate(m_Url);
		}

		private void menuLocation_Click(object sender, System.EventArgs e)
		{
			if (LocationString!="")
				MessageBox.Show(LocationString,"Location");		
		}

		private void HTMLView_GotFocus(object sender, System.EventArgs e)
		{		
			this.webBrowser1.Focus();			
		}
	}
}
